package com.example.zhang;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class Final_work extends ListActivity implements Runnable, AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    private static final String TAG = "a";//控制台信息输出
    private ArrayList<HashMap<String, String>> listItems;
    private ArrayList<HashMap<String, String>> listItems1;
    Handler handler;
    String time_get = "123";
    SimpleAdapter listItemadapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_final_work);
        listItems = new ArrayList<HashMap<String, String>>();
        listItems1 = new ArrayList<HashMap<String, String>>();
       // listItems1 = new ArrayList<HashMap<String, String>>();
        SharedPreferences sharedPreferences = getSharedPreferences("date_final", Activity.MODE_PRIVATE);
        PreferenceManager.getDefaultSharedPreferences(this);
        time_get = sharedPreferences.getString("time_save", "123");

        Thread t = new Thread((Runnable) this);//开启后会启动子线程中的run方法.
        t.start();
        handler = new Handler(){
            @SuppressLint("HandlerLeak")
            @Override
            public void handleMessage(@NonNull Message msg){
                if(msg.what==1){
                    listItems = (ArrayList<HashMap<String, String>>)msg.obj;
                    listItemadapter = new SimpleAdapter(Final_work.this,
                            listItems,
                            R.layout.final_item,
                            new String[]{"ItemTitle", "ItemDetail"},
                            new int[]{R.id.words, R.id.marked});
                    setListAdapter(listItemadapter);
                }
                super.handleMessage(msg);
            }
        };
        getListView().setOnItemClickListener(this);
        getListView().setOnItemLongClickListener(this);


    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView title = (TextView) view.findViewById(R.id.words);
        TextView detail = (TextView) view.findViewById(R.id.marked);
        String words = String.valueOf(title.getText());
        Log.i(TAG, "onItemClick: title2="+words);
        // 打开新的页面
//        Intent ratecalc = new Intent(this,rate_cal.class);
//        startActivity(ratecalc);
    }
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position,long id){
        Log.i(TAG, "wuwuwu");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setNegativeButton("否",null)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listItems.remove(position);
                        listItemadapter.notifyDataSetChanged();
                    }
                });
        builder.create().show();
        return true;
    }

    public void run() {
        Date time_now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


//        if (time_get.equals(format.format(time_now))) {
//            Log.i(TAG, "今天已经更新过了！！！" + "程序更新时间是：" + time_get);
//        } else {
            try {

                String uu[] = {"181", "181_2", "181_3", "181_4"};
                for (String i : uu) {
                    Document doc = Jsoup.connect("http://www.yywords.com/Article/200803/" + i + ".html").get();

                    Element dc = doc.select("#fontzoom").first();//单词
                    Elements wq = dc.getElementsByTag("p");
                    for (Element we : wq) {
                        Elements wr = we.getElementsByTag("span");
                        Elements wt = we.getElementsByTag("b");
                        if (wr.size() > 0 && wt.size() > 0 && !wr.text().isEmpty() && !wt.text().isEmpty() && !we.text().matches("[\\p{Upper}]")) {
                            //Log.i(TAG, "iiii    " + we.text());
                            HashMap<String, String> map1 = new HashMap<String, String>();
                            map1.put("ItemTitle", we.text());
                            map1.put("ItemDetail", "");
                            listItems1.add(map1);
                        }
                    }
                }

                SharedPreferences sp = getSharedPreferences("date_final", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("time_save", format.format(time_now));
                editor.apply();
                 Log.i(TAG, "完成每日更新" + format.format(time_now));

            } catch (IOException e) {
                e.printStackTrace();
            }
            Message msg1 = handler.obtainMessage(1);
            msg1.obj =listItems1;
            handler.sendMessage(msg1);
        }
    }
